package com.lyf.core.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.lyf.core.pojo.Role;

@Repository
public interface RoleDao {

	@Insert("insert into ma_role(role_name) values(#{role.role_name})")
	@Options(useGeneratedKeys = true, keyProperty = "role.role_id")
	public void insert(@Param("role") Role role);

	@Select("select * from ma_role where role_name=#{roleName} limit 1")
	public Role findByName(@Param("roleName") String roleName);
	
	@Select("select * from public.ma_role as t1 where t1.role_id in  (select regexp_split_to_table(roles , ',')::int from ma_user as t2 where t2.user_id = #{user_id})")
	public List<Role> findUserRoles(@Param("user_id") String user_id);
	
	
	@Delete("delete from ma_role where role_id = #{role_id};")
	public void delete(@Param("role_id") int role_id);
	
	@Delete("update ma_role set role_name=#{r.role_name} where role_id = #{r.role_id};")
	public void update(@Param("r") Role r);
	
	@Select("select * from ma_role ")
	public List<Role> findAll();
	
	@Update("update ma_role set perms = #{perms} where role_id = #{role_id} ")
	public void updateRolePerms(@Param("role_id")int role_id ,@Param("perms") String perms);
	
}

